package fr.iut2.tc1.projet.client;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
 * ListeSponsor est une classe qui permet de gérer la liste
 * de sponsors affichée sur la gauche.*/
public class ListeSponsor extends Composite {
	private ScrollPanel listPanel;
	private VerticalPanel images;
	private Image imgCourante;
	private SelectList selection;
	private final ServicesAsync services = GWT.create(Services.class);
	
	public ListeSponsor(){
		AsyncCallback<ArrayList<SponsorLot>> callbackSponsorLot = new AsyncCallback<ArrayList<SponsorLot>>(){
			public void onFailure(Throwable caught) {
				System.out.println("pb getlistsponsors");
			}

			@Override
			public void onSuccess(ArrayList<SponsorLot> result) {
				for(SponsorLot s : result){
					System.out.println(s.getUrlLogo());
					SimplePanel logo = new SimplePanel();
					Image img = new Image(s.getUrlLogo());
					img.setPixelSize(100, 100);
					selection= new SelectList(s);
					
					img.addClickHandler(selection);
					
					img.addClickHandler( new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							imgCourante.setPixelSize(100, 100);
							imgCourante = ((Image)event.getSource());
							imgCourante.setPixelSize(120, 120);
						}
					});
					
					logo.setWidget(img);
					images.setHeight("160px");
					images.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
					images.setVerticalAlignment(VerticalPanel.ALIGN_MIDDLE);
					images.setSpacing(10);
					images.add(logo);
				}
			}
		};
		
		AsyncCallback<ArrayList<SponsorArgent>> callbackSponsorArgent = new AsyncCallback<ArrayList<SponsorArgent>>(){
			public void onFailure(Throwable caught) {
				System.out.println("pb getlistsponsors");
			}

			@Override
			public void onSuccess(ArrayList<SponsorArgent> result) {
				for(SponsorArgent s : result){
					System.out.println(s.getUrlLogo());
					SimplePanel logo = new SimplePanel();
					Image img = new Image(s.getUrlLogo());
					img.setPixelSize(100, 100);
					selection= new SelectList(s);
					
					img.addClickHandler(selection);
					
					img.addClickHandler( new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							imgCourante.setPixelSize(100, 100);
							imgCourante = ((Image)event.getSource());
							imgCourante.setPixelSize(120, 120);
						}
					});
					
					logo.setWidget(img);
					images.setHeight("160px");
					images.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
					images.setVerticalAlignment(VerticalPanel.ALIGN_MIDDLE);
					images.setSpacing(10);
					images.add(logo);
				}
			}
		};
		
		listPanel = new ScrollPanel();
		/**
		 * On crée un bouton suivant permettant de faire défiler
		 * la liste des sponsors*/
		
		
		images = new VerticalPanel();
		imgCourante= new Image();
		listPanel.scrollToTop();
		//listPanel.setSize("100%", "150px");
		listPanel.add(images);
		//listPanel.setHeight("140px");
		listPanel.setSize("200px", "400px");
		
		services.getListSponsorsLot(callbackSponsorLot);
		services.getListSponsorsArgent(callbackSponsorArgent);

		
		initWidget(listPanel);
	}
	
	public SelectList getSelection() {
		return selection;
	}
}
